package com.lyshop.jinxiang.view;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.activitys.WebViewActivity;
import com.lyshop.jinxiang.application.MyApplication;
import com.lyshop.jinxiang.constants.Constant;
import com.lyshop.jinxiang.core.AppAction;


public class LoginRegisterPrivateDialog extends MyDialog implements View.OnClickListener {

    private View rootView;
    private TextView tv_register_text;
    private TextView tv_cancel;
    private TextView tv_confirm;
    protected AppAction mAppAction;


    public LoginRegisterPrivateDialog(Context context, PrivateConfirmListener listener) {
        this(context, false, Gravity.CENTER,listener);
    }

    public LoginRegisterPrivateDialog(Context context, boolean isCancelAble, int gravity, PrivateConfirmListener listener) {
        super(context, isCancelAble, gravity);
        this.mListener = listener;
        mAppAction = MyApplication.getApplication().getAppAction();
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_login_private, null);
        setContentView(rootView);

        tv_register_text = rootView.findViewById(R.id.tv_register_text);
        tv_cancel = rootView.findViewById(R.id.tv_cancel);
        tv_confirm = rootView.findViewById(R.id.tv_confirm);

        String text = "请您谨慎阅读并充分理解《用户协议》和《隐私政策》内容。为了能够给您提供更好服务，请您认真阅读，如你同意，请点击“同意”开始接受我们的服务";

        SpannableString spannableString = new SpannableString(text);

        int userAgreementStart = text.indexOf("《用户协议》");
        int userAgreementEnd = userAgreementStart + 5;
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Bundle bundle = new Bundle();
                bundle.putString("url", Constant.H5_USER_URL);
                gotoActivity(mContext, WebViewActivity.class, bundle);
            }
        }, userAgreementStart, userAgreementEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.color_text_165dff)), userAgreementStart, userAgreementEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        int privacyPolicyStart = text.indexOf("《隐私政策》");
        int privacyPolicyEnd = privacyPolicyStart + 5;
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Bundle bundle = new Bundle();
                bundle.putString("url", Constant.H5_PRIVATE_URL);
                gotoActivity(mContext, WebViewActivity.class, bundle);
            }
        }, privacyPolicyStart, privacyPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.color_text_165dff)), privacyPolicyStart, privacyPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv_register_text.setText(spannableString);
        tv_register_text.setMovementMethod(LinkMovementMethod.getInstance()); // 允许点击链接

        tv_cancel.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Bundle bundle;
        switch (v.getId()) {
            case R.id.tv_user_url:
                bundle = new Bundle();
                bundle.putString("url", Constant.H5_USER_URL);
                gotoActivity(mContext, WebViewActivity.class, bundle);
                break;
            case R.id.tv_private_url:
                bundle = new Bundle();
                bundle.putString("url", Constant.H5_PRIVATE_URL);
                gotoActivity(mContext, WebViewActivity.class, bundle);
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                dismiss();
                mListener.onConfirmListener();
                break;
        }
    }


    public void gotoActivity(Context mContext, Class<?> toActivityClass, Bundle bundle) {
        Intent intent = new Intent(mContext, toActivityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        mContext.startActivity(intent);
        ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);
    }


    private PrivateConfirmListener mListener;

    public interface PrivateConfirmListener {
        void onConfirmListener();
    }
}
